package com.ts.api.http.platform.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.*;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.http.platform.entity.apply.ApiPlatApplyRongHuiFenQi;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.*;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;

import java.util.Map;

/**
 *
 *  融汇分期  - 撞库百分百成功
 *
 *  全流程  链接：https://www.yuque.com/bintang-xyzcb/dnpq52/kmlhzov9dttt0w46?singleDoc#
 *          密码：fiip
 * @author gwc
 * @create 2025-08-06 下午4:56
 */

@ApiPlan(productId = "396", type = ApiPlanConvertType.PLAT_PHONE_MD5)
public class ApiPlatRongHuiFenQiPhoneMd5Convert implements ApiPlatformPhoneMd5ConvertHandler {

    private static final String CHANNEL = "t0p6but";
    private static final String KEY = "fc54c9c80fc11849fd1838fb32aa4bb4";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {

        return Map.of();
    }

    @Override
    public RespPlatformPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        //  撞库返回响应体判断
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1)) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }

        RespPlatformPhoneMd5DataExchange result = RespPlatformPhoneMd5DataExchange.ofSuc();
        result.setDisplayName("融汇分期");
        result.setProductName("融汇分期");
        return result;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        // 创建进件实体类
        ApiPlatApplyRongHuiFenQi rongHuiFenQiData = new ApiPlatApplyRongHuiFenQi();
        // 用户姓名
        rongHuiFenQiData.setName(apiApplyParameter.getUserName());
        // 手机号
        rongHuiFenQiData.setMobile(apiApplyParameter.getPhone());
        // 城市/code，如：广州市 或 4401
        rongHuiFenQiData.setAddress(apiApplyParameter.getCity());
        // 年龄，如：25
        rongHuiFenQiData.setAge(apiApplyParameter.getAge());
        // 性别：【1】男 【2】女
        String sex = ChooseEq.create(apiApplyParameter.getSex())
                .when("0", "1")
                .when("1", "2")
                .end("1");
        rongHuiFenQiData.setGender(sex);
        // 贷款额度（单位：万）
        String money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", "3")
                .when(apiApplyParameter.getLoanAmount(), "2", "5")
                .when(apiApplyParameter.getLoanAmount(), "3", "10")
                .when(apiApplyParameter.getLoanAmount(), "4", "20")
                .end("3");
        rongHuiFenQiData.setLoan_money(money);
        // 芝麻分：【0】无 【1】600以下 【2】600-649 【3】650-699 【4】700以上
        String zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", "1")
                .when(apiApplyParameter.getSesameCredit(), "2", "2")
                .when(apiApplyParameter.getSesameCredit(), "3", "3")
                .when(apiApplyParameter.getSesameCredit(), "4", "4")
                .end("0");
        rongHuiFenQiData.setSesame_score(zhima);
        // 花呗额度：【0】无 【1】0-5000 【2】5000以上
        String hbLimit = ChooseEq.create()
                .when(apiApplyParameter.getHbLimit(), "1", "0")
                .when(apiApplyParameter.getHbLimit(), "2", "1")
                .when(apiApplyParameter.getHbLimit(), "3", "2")
                .when(apiApplyParameter.getHbLimit(), "4", "2")
                .when(apiApplyParameter.getHbLimit(), "5", "2")
                .end("0");
        rongHuiFenQiData.setHuabei_quota(hbLimit);
        // 白条额度：【0】无 【1】0-5000 【2】5000以上
        String btLimit = ChooseEq.create()
                .when(apiApplyParameter.getBtLimit(), "1", "0")
                .when(apiApplyParameter.getBtLimit(), "2", "1")
                .when(apiApplyParameter.getBtLimit(), "3", "2")
                .when(apiApplyParameter.getBtLimit(), "4", "2")
                .when(apiApplyParameter.getBtLimit(), "5", "2")
                .end("0");
        rongHuiFenQiData.setBaitiao_quota(btLimit);
        // 车产：【0】无 【1】有
        String car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", "0")
                .when(apiApplyParameter.getCarInfo(), "2", "1")
                .when(apiApplyParameter.getCarInfo(), "3", "1")
                .when(apiApplyParameter.getCarInfo(), "4", "1")
                .end("0");
        rongHuiFenQiData.setCar(car);
        // 房产：【0】无 【1】有
        String house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", "0")
                .when(apiApplyParameter.getRealEstateInfo(), "2", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "3", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "4", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "5", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "6", "1")
                .end("0");
        rongHuiFenQiData.setHouse(house);
        // 公积金：【0】无 【1】有
        String gongjijin = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", "0")
                .when(apiApplyParameter.getProvidentFund(), "2", "1")
                .when(apiApplyParameter.getProvidentFund(), "3", "1")
                .when(apiApplyParameter.getProvidentFund(), "4", "1")
                .end("0");
        rongHuiFenQiData.setAccumulation_fund(gongjijin);
        // 社保：【0】无 【1】有
        String shebao = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", "0")
                .when(apiApplyParameter.getSocialSecurity(), "2", "1")
                .when(apiApplyParameter.getSocialSecurity(), "3", "1")
                .when(apiApplyParameter.getSocialSecurity(), "4", "1")
                .end("0");
        rongHuiFenQiData.setSocial_security(shebao);
        // 保单：【0】无 【1】有
        String baodanIs = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", "0")
                .when(apiApplyParameter.getInsurancePolicy(), "2", "1")
                .when(apiApplyParameter.getInsurancePolicy(), "3", "1")
                .when(apiApplyParameter.getInsurancePolicy(), "4", "1")
                .end("0");
        rongHuiFenQiData.setInsurance(baodanIs);
        // 营业执照：【0】无 【1】有
        String bizLicenseYear = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", "0")
                .when(apiApplyParameter.getBizLicenseYear(), "2", "1")
                .when(apiApplyParameter.getBizLicenseYear(), "3", "1")
                .when(apiApplyParameter.getBizLicenseYear(), "4", "1")
                .end("0");
        rongHuiFenQiData.setBusiness_owner(bizLicenseYear);
        // 职业：【0】无 【1】公务员 【2】事业单位 【3】民营 【4】国企
        String job = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", "3")
                .when(apiApplyParameter.getCareer(), "2", "0")
                .when(apiApplyParameter.getCareer(), "3", "3")
                .when(apiApplyParameter.getCareer(), "4", "1")
                .when(apiApplyParameter.getCareer(), "5", "3")
                .end("0");
        rongHuiFenQiData.setWork(job);
        // 信用状况：【0】无 【1】当前无逾期 【2】当前有逾期 【3】逾期已结清
        String overdue = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", "1")
                .when(apiApplyParameter.getOverdue(), "2", "2")
                .when(apiApplyParameter.getOverdue(), "3", "1")
                .end("0");
        rongHuiFenQiData.setCredit_status(overdue);
        // 加密
        AES aes = AES.createEcb(KEY.getBytes());
        String data = aes.encryptToBase64Str(JsonUtils.toJson(rongHuiFenQiData));
        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channel", CHANNEL);
            newMap.put("content", data);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespPlatformPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        // 获取响应数据
        JSONObject resData = jsonObject.getJSONObject("data");
        if (ValidParam.isEmpty(resData)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if(!Eq.object(resData.getInteger("status"), 1)){
            return RespPlatformPhoneMd5Apply.ofFail();
        }

        return RespPlatformPhoneMd5Apply.ofSuc();

    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        return null;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        return Map.of();
    }
}
